<?php
/**
 * 定义 Loader 类
 * 
 * @copyright Copyright (c) 2008 Classic Reappear Technology LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @version $Id: loader.php 2009-2-27 下午02:00:43 蓝色身份 $
 */

/**
 * 一组用于加载 PHP 类和文件的静态函数
 *
 * @author 蓝色身份 <lance.w@live.cn>
 * @copyright Copyright (c) 2008 Classic Reappear Technology LLC
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @package Loader
 */
class Loader
{
    /**
     * 从指定的 PHP 文件加载一个类
     *
     * @param string $class 类的名称
     * @param string|array $dirs 文件的搜索路径
     * @return boolean
     */
    public static function loadClass($class, $dirs = null)
    {
        if (class_exists($class, false) || interface_exists($class, false)) {
            return;
        }

        if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) {
            throw new Exception('Directory argument must be a string or an array');
        }

        $file = str_replace('_', DIRECTORY_SEPARATOR, strtolower($class)) . '.php';
        if (!empty($dirs)) {
            $dirPath = dirname($file);
            if (is_string($dirs)) {
                $dirs = explode(PATH_SEPARATOR, $dirs);
            }
            foreach ($dirs as $key => $dir) {
                if ($dir == '.') {
                    $dirs[$key] = $dirPath;
                } else {
                    $dir = rtrim($dir, '\\/');
                    $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath;
                }
            }
            $file = basename($file);
        }

        return self::loadFile($file, $dirs);
    }

    /**
     * 加载一个 PHP 文件
     *
     * @param string $filename 文件名称
     * @param string|array $dirs 文件的搜索路径
     * @return boolean
     */
    public static function loadFile($filename, $dirs = null)
    {
        if (preg_match('/[^a-z0-9\\/\\\\_.-]/i', $filename)) {
            throw new Exception('Security check: Illegal character in filename');
        }

        $incPath = false;
        if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {
            if (is_array($dirs)) {
                $dirs = implode(PATH_SEPARATOR, $dirs);
            }
            $incPath = get_include_path();
            set_include_path($dirs . PATH_SEPARATOR . $incPath);
        }

		include_once $filename;

        if ($incPath) {
            set_include_path($incPath);
        }

        return true;
    }

    /**
     * 尝试加载指定的类
     *
     * @param string $class 类的名称
     * @return mixed
     */
    public static function autoload($class)
    {
        try {
            self::loadClass($class);
            return $class;
        } catch (Exception $e) {
            return false;
        }
    }

    /**
     * 注册自动加载 PHP 类的函数
     *
     * @param string $class 类的名称
     */
    public static function registerAutoload($class = 'Loader')
    {
        if (!function_exists('spl_autoload_register')) {
            throw new Exception('spl_autoload does not exist in this PHP installation');
        }

        self::loadClass($class);
        $methods = get_class_methods($class);
        if (!in_array('autoload', (array) $methods)) {
            throw new Exception("The class \"$class\" does not have an autoload() method");
        }

        spl_autoload_register(array($class, 'autoload'));
    }
}